<template>
	<div class="sys-user-container">
		<el-dialog v-model="state.isShowDialog"  draggable :close-on-click-modal="false" width="700px">
			<template #header>
				<div style="color: #fff">
					<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>
					<span>{{ props.title }}</span>
				</div>
			</template>
			<div style="height: 550px; margin-left: 36px">
					<el-transfer :data="state.roleData" :props="{ key: 'id', label: 'name' }" v-model="state.ruleForm.roleIdList" :titles="['未授权', '已授权']"></el-transfer>
			</div>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="sysEditUser">
import { onMounted, reactive, ref } from 'vue';
import {apiSysRoleListGet,apiSysUserOwnRoleListUserIdGet,apiGrantRolePost } from '/@/api-services/api';

const props = defineProps({
	title: String,
});
const emits = defineEmits(['handleQuery']);
const state = reactive({
	loading: false,
	isShowDialog: false,
	ruleForm: {} as any,
	roleData: [] as Array<any>, // 角色数据
});
// 级联选择器配置选项
const cascaderProps = { checkStrictly: true, emitPath: false, value: 'id', label: 'name', expandTrigger: 'hover' };

onMounted(async () => {
	state.loading = true;
	var res1 = await apiSysRoleListGet();
	state.roleData = res1.data.data ?? [];
	state.loading = false;
});

// 打开弹窗
const openDialog = async (row: any) => {
	state.roleData =(state.roleData) ?? [];
	state.ruleForm = JSON.parse(JSON.stringify(row));
	var resRole = await  apiSysUserOwnRoleListUserIdGet(row.id);
	state.ruleForm.roleIdList = resRole.data.data;
	state.isShowDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emits('handleQuery');
	state.isShowDialog = false;
};

// 取消
const cancel = () => {
	state.isShowDialog = false;
};

// 提交
const submit =(async () => {
	var body={userId:state.ruleForm.id,roleIdList:state.ruleForm.roleIdList};
	if (state.ruleForm.id != undefined && state.ruleForm.id > 0)
		await apiGrantRolePost(body);
	else
		return;
	closeDialog();
});

// 导出对象
defineExpose({ openDialog });
</script>
